package com.molinari.coding.activities.addconto;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.molinari.androidstructure.widget.arrayAdapter.ArrayAdapterCustom;
import com.molinari.coding.Cella;
import com.molinari.coding.R;

public class CelleAdapter extends ArrayAdapterCustom<Cella> {

	private Cella[] celle;
	
	public CelleAdapter(Context context, 
						Activity activity, 
						int resource,
						Cella[] objects) {
		
		super(context, activity, resource, objects);
		this.celle = objects;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = super.getView(position, convertView, parent);
		RowMomento memento = (RowMomento) convertView.getTag();
		memento.uno.setText(celle[position].getUno());
		memento.due.setText(celle[position].getDue());
		memento.tre.setText(celle[position].getTre());
		return convertView;
	}
	
	@Override
	protected Object cercaViewMemento(View convertView, boolean viewEsistente, int position) {
		RowMomento memento = (RowMomento) super.cercaViewMemento(convertView, viewEsistente, position);
		memento.uno.setTag(celle[position]);
		memento.due.setTag(celle[position]);
		memento.tre.setTag(celle[position]);
		return memento;
	}

	@Override
	public void aggiornaViewMemento(Cella item, Object viewMemento, int position) {
		final RowMomento rowMomento = (RowMomento) viewMemento;
		rowMomento.chiave.setText(item.getChiave());
		rowMomento.uno.setText(celle[position].getUno());
		rowMomento.due.setText(celle[position].getDue());
		rowMomento.tre.setText(celle[position].getTre());
		
		
		int index = Integer.parseInt(rowMomento.chiave.getText().toString()) - 1;
		celle[index].setUno(rowMomento.uno.getText().toString());
		celle[index].setDue(rowMomento.due.getText().toString());
		celle[index].setTre(rowMomento.tre.getText().toString());
		//CelleAdapter.this.notifyDataSetChanged();
			
		rowMomento.uno.addTextChangedListener(new CustomTextWatcher(rowMomento.uno, celle,item, 1));
		rowMomento.due.addTextChangedListener(new CustomTextWatcher(rowMomento.due, celle, item, 2));
		rowMomento.tre.addTextChangedListener(new CustomTextWatcher(rowMomento.tre, celle,item, 3));
	}

	@Override
	public Object initViewMemento(View convertView) {
		RowMomento rowMomento = new RowMomento();
		rowMomento.chiave = (TextView) convertView.findViewById(R.id.textNumber);
		rowMomento.uno = (EditText) convertView.findViewById(R.id.editAggiungiPrimo);
		rowMomento.due = (EditText) convertView.findViewById(R.id.editAggiungiSecondo);
		rowMomento.tre = (EditText) convertView.findViewById(R.id.editAggiungiTerzo);
		return rowMomento;
	}
	
	class RowMomento {
		public TextView chiave;
		public EditText uno;
		public EditText due;
		public EditText tre;
	}
	class CustomTextWatcher implements TextWatcher{
		
		EditText edit;
		Cella[] celle;
		Cella cella;
		int position;

		public CustomTextWatcher(EditText edit, Cella[] celle,Cella cella, int position) {
			this.edit = edit;
			this.celle = celle;
			this.cella = cella;
			this.position = position;
		}

		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,int after) {
			
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,int count){ 
			Cella element=(Cella)edit.getTag(); 
			if(position == 1){
				element.setUno(s.toString());
			}else if(position == 2){
				element.setDue(s.toString());
			}else if(position == 3){
				element.setTre(s.toString());
			}
		}
	}

}
